/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package optimizacion.problemas;

import ec.util.MersenneTwisterFast;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author USUARIO
 */
public class TSPProblem {
    
    public static void main(String[] args) {
        String ruta = "src\\optimizacion\\resources\\berlin52.tsp";
        int numeroNodos= 52;
        TSPFileReader lector = new TSPFileReader(0, new double[numeroNodos][2], ruta);
        try {
            MersenneTwisterFast prng = new MersenneTwisterFast(321654987);
            int n = 1000000;
            lector.readFile();
            double[][] nodos = lector.getCoordinates();
            int[] solucion = new int[52];
            int[] solucionOptima = new int[52];
            double distanciaOptima = Double.MAX_VALUE;
            double distancia;
            int nodo;
            for (int i = 0; i < n; i++) {
//                System.out.println("--------------------------------------------");
                distancia = 0;
                nodo = (int)Math.floor(prng.nextDouble()*52);
                solucion[0] = nodo;
//                System.out.println(1 + " : " + solucion[0]);
                for (int j = 1; j < numeroNodos ; j++) {
                    boolean esta;
                    do {
                        esta = false;
                        nodo = (int)Math.floor(prng.nextDouble()*52);
                        for (int k = 0; k < j; k++) {
                            if (nodo == solucion[k]) {
                                esta = true;
                                break;
                            }
                        }
                    } while (esta);
                    solucion[j] = nodo;
//                    System.out.println((j+1) + " : " + solucion[j]);
                    distancia += Math.sqrt((nodos[j][0]- nodos[j-1][0])*(nodos[j][0]- nodos[j-1][0]) 
                                        + (nodos[j][1]- nodos[j-1][1])*(nodos[j][1]- nodos[j-1][1]));
                }
                if (distancia < distanciaOptima) {
                    distanciaOptima = distancia;
                    solucionOptima = solucion;
                }
            }
            System.out.println("\n\nsolucion: ");
            for (int i = 0; i < solucionOptima.length; i++) {
                System.out.print(solucionOptima[i] + " , ");
            }
            System.out.println("\n\nditancia: " + distanciaOptima);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TSPProblem.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(TSPProblem.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TSPProblem.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
